{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cdb8a855",
   "metadata": {},
   "outputs": [],
   "source": [
    "#项目7-例7-3\n",
    "#数据准备\n",
    "from sklearn.datasets import load_breast_cancer #导入肺癌数据集\n",
    "from sklearn.svm import SVC     #导入支持向量机分类模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "#import pandas as pd\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "x,y=load_breast_cancer().data,load_breast_cancer().target \n",
    "x=StandardScaler().fit_transform(x)#数据标准化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c5734279",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAZsklEQVR4nO3da5Cc1X3n8e9vbpqLLtNjBiGNBBJZcVHAXDyWk2VtYxNjEcdR8NbuCmfjFLVZlWqRl6Q22WDvi9RW3njDJrWuhUSldTDZim0F22gtb1QIl42N14XtGUAgDSB7IrA0DFgjCwSMBKOZ+e+L7hHtpkfzSOpWT5/+faqomX6ec7r/D5cfR6fPcx5FBGZmlq6mWhdgZmbV5aA3M0ucg97MLHEOejOzxDnozcwS11LrAsq54IILYtWqVbUuw8ysbjz++ONHIqK33Ll5GfSrVq1icHCw1mWYmdUNST+b7ZynbszMEuegNzNLnIPezCxxDnozs8Q56M3MEuegNzNLnIPezCxxSQX9z34xzvd/OlbrMszM5pV5ecPU2frg3d8F4IXPfay2hZiZzSNJjejNzOydkgz6Q0eP17oEM7N5I8mgf/9fPFLrEszM5o0kg97MzN6WbNC/fOzNWpdgZjYvJBX0C1revpxH9h+uYSVmZvNHUkH/wct6ueKiRVy2dCH/MHCo1uWY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RLEfFE4ffXyf/H31fbqqpP0grgY8AXal3L+SBpMfAB4G8BImIiIl6taVHnRwvQUXiUaSdVfNZ0rUTEo8DRksMbgL8r/P53wO9U4rNSCfo+4FDR6xEaIPRmSFoFXAf8qMalnA//A/jP5B9Z2QguBcaALxamq74gqavWRVVTRLwI/HfgIPAScCwiHq5tVefN0oh4CfKDOeDCSrxpKkGvMscaYt2opIXA14E/jIjXal1PNUn6LeBwRDxe61rOoxbgeuBvIuI6YJwK/XF+virMS28AVgPLgS5J/7a2VdW3VIJ+BFhZ9HoFCf5Rr5SkVvIh/6WIeLDW9ZwHNwC/LekF8tNzH5b097UtqepGgJGImPnT2tfIB3/KfgN4PiLGIuIk8CDwz2tc0/nyc0nLAAo/D1fiTVMJ+gFgjaTVktrIf3Gzs8Y1VZUkkZ+3fTYi/qrW9ZwPEfGZiFgREavI/zP+TkQkPdKLiJeBQ5IuLxy6CXimhiWdDweBX5PUWfj3/CYS/wK6yE7g9wu//z7wjUq8aUsl3qTWImJS0hZgN/lv6O+LiKEal1VtNwC/B+yVtKdw7LMRsat2JVmVfBr4UmEQcwC4vcb1VFVE/EjS14AnyK8ue5IEt0OQ9BXgRuACSSPAnwGfAx6Q9O/I/w/vX1Xks7wFgplZ2lKZujEzs1k46M3MEuegNzNLnIPezCxxDnozs8Q56M3MEuegNzNL3P8HdbF8ZZ1hmvEAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "参数gamma的最优值为：0.012067926406393264\n",
      "选取该参数值时，模型的预测准确率为：0.976608\n"
     ]
    }
   ],
   "source": [
    "#找到最优的gamma参数\n",
    "from sklearn.metrics import accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "#分割数据集\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=420)\n",
    "\n",
    "score=[]\n",
    "gamma_range=np.logspace(-10,1,50)\n",
    "for i in gamma_range:\n",
    "    model=SVC(kernel='rbf',gamma=i)\n",
    "    model.fit(x_train,y_train)\n",
    "    pred=model.predict(x_test)\n",
    "    ac=accuracy_score(y_test,pred)\n",
    "    score.append(ac)\n",
    "#画曲线图，横轴为gamma值，纵轴为对应模型的预测准确率\n",
    "plt.plot(gamma_range,score)\n",
    "plt.show()\n",
    "#输出模型的最大预测准确率与对应的gamma值\n",
    "print(\"参数gamma的最优值为：%s\"%gamma_range[score.index(max(score))])\n",
    "print(\"选取该参数值时，模型的预测准确率为：%f\"%max(score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3aacb8d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
